package com.ymatou.iapi.productmanager.testcase;

import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.AddSellerBrandBean;
import com.ymatou.iapi.productmanager.service.AddSellerBrandCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.compare.AssertHelper;


public class Ts_AddSellerBrand {
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static AddSellerBrandCall addSellerBrandCall = new AddSellerBrandCall();
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手品牌提报");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addSellerBrandCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_001() {
		Logger.start(true, "买手品牌提报");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			checkAddSellerBrand(bean,ret);
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_002() {
		Logger.start(true, "买手品牌提报,部分信息没有");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			bean.setCnName(null);
			bean.setDescription(null);
			bean.setPicUrl(null);
			
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			checkAddSellerBrand(bean,ret);
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_003() {
		Logger.start(true, "没传买手id");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			bean.setSellerId(0);
			
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(400, ret.getInt("Code"), "Check return code");
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_004() {
		Logger.start(true, "没传brandEn");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			
			bean.setEnName("");
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(400, ret.getInt("Code"), "Check return code");
			
			bean.setEnName(null);
			JSONObject ret2 = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(400, ret2.getInt("Code"), "Check return code");
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_005() {
		Logger.start(true, "重复提交同一名称品牌");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			checkAddSellerBrand(bean,ret);
			
			JSONObject ret2 = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(201, ret2.getInt("Code"), "Check return code");
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_006() {
		Logger.start(true, "提交已存在名称品牌");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			bean.setEnName("IBM");
			
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(201, ret.getInt("Code"), "Check return code");
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerBrand_007() {
		Logger.start(true, "没传sellerName");
		try{
			AddSellerBrandBean bean = getAddSellerBrandBean(sellerId,sellerName);
			
			bean.setSellerName("");
			JSONObject ret = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(400, ret.getInt("Code"), "Check return code");
			
			bean.setSellerName(null);
			JSONObject ret2 = addSellerBrandCall.callAndGetReturnData(bean);
			Logger.verifyEquals(400, ret2.getInt("Code"), "Check return code");
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	
	
	
	public static AddSellerBrandBean getAddSellerBrandBean(int sellerId,String sellerName){
		AddSellerBrandBean brandBean = new AddSellerBrandBean();
		brandBean.setSellerId(sellerId);
		brandBean.setSellerName(sellerName);
		brandBean.setEnName(TestDataManager.randomStr(18));
		brandBean.setCnName("创建新品牌");
		brandBean.setDescription("创建新品牌描述");
		brandBean.setPicUrl("http://pic1.ymatou.com/G01//M00/3A/B5/rBBlD1n4FoqAEeSFAAdYEcXNxCc313.png");
		return brandBean;
	}
	
	
	
	public static void checkAddSellerBrand(AddSellerBrandBean requestBean,JSONObject ret) throws JSONException{
		Logger.verifyEquals(200, ret.getInt("Code"), "check return code");
		
		
		List<Map<String,Object>> brandInSql = intergratedProductMapper.selectApplyBrandsByBrandEn(requestBean.getEnName());
		Logger.verifyEquals(1, brandInSql.size(), "brand count should be as one");
		
		AddSellerBrandBean actBean = new AddSellerBrandBean();
		actBean.setSellerId((int)brandInSql.get(0).get("SellerId"));
		actBean.setSellerName(brandInSql.get(0).get("SellerName")==null?null:brandInSql.get(0).get("SellerName").toString());
		actBean.setEnName(brandInSql.get(0).get("EnName").toString());
		actBean.setCnName(brandInSql.get(0).get("CnName")==null?null:brandInSql.get(0).get("CnName").toString());
		actBean.setPicUrl(brandInSql.get(0).get("PicUrl")==null?null:brandInSql.get(0).get("PicUrl").toString());
		actBean.setDescription(brandInSql.get(0).get("Description")==null?null:brandInSql.get(0).get("Description").toString());
		
		AssertHelper.assertResultEqual(requestBean, actBean, "checkAddSellerBrandcheckAddSellerBrand");
		
	}
	

}
